package cn.lingyangwl.agile.system.controller;

import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.sign.SignPayload;
import cn.lingyangwl.framework.sign.SignType;
import cn.lingyangwl.framework.sign.SignUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.lingyangwl.agile.common.security.annotation.Anonymous;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.github.yitter.idgen.YitIdHelper;
import io.swagger.annotations.Api;
import lombok.Data;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author shenguangyang
 */
@Anonymous
@Api(tags = "API访问密钥")
@RestController
@RequestMapping("/testSign")
public class TestSignController {

    @Data
    public static class TestData03 {
        private String e = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss");
    }

    @Data
    public static class TestData02 {
        private String b = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss");
        private String a = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd");
        private String c = DateUtil.format(LocalDateTime.now(), "HH:mm:ss");
        private TestData03 testData03 = new TestData03();
    }

    @Data
    public static class TestData {
        /** 用户ID */
        private Long id = YitIdHelper.nextId();

        /** 工号 */
        private String workNumber = RandomUtil.randomNumbers(16);

        private String belongInfoId = RandomUtil.randomNumbers(16);

        /** 当前的部门id */
        private Long currentDeptId = YitIdHelper.nextId();

        /** 当前租户id */
        private Long currentTenantId = YitIdHelper.nextId();

        /** 当前租户下的系统应用id */
        private Long currentAppId = YitIdHelper.nextId();

        /** 用户账号 */
        private String userName = "admin";

        /** 用户真实姓名 */
        private String realName = "admin";

        /** 用户昵称 */
        private String nickName = "test-admin";

        /** 用户邮箱 */
        private String email = "shenguangyang@foxmail.com";

        /** 手机号码 */
        private String phone = "12314235235345";

        /** 用户性别, 1=男,0=女,-1=未知 */
        private String sex = "2";

        /** 帐号状态（1正常 2停用） */
        private String status = "1";

        /** 删除标志（0代表存在 2代表删除） */
        private String delFlag = "0";

        /** 最后登录IP */
        private String loginIp = "127.0.0.1";

        /** 最后登录时间 */
//        @JSONField(format="yyyy-MM-dd HH:mm:ss")
        private String loginDate = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss");


        /** 所属部门, 多个部门之间使用逗号隔开 */
        private String deptNames = "test1,test2";

        /** 所属部门, 多个部门之间使用逗号隔开 */
        private List<Long> deptIds = Arrays.asList(1L, 2L);

        /**
         * 用户生日
         */
        private String birthday = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss");

        private TestData02 testData02 = new TestData02();
    }

    @PostMapping("/post")
    public Resp<?> test01(@RequestBody TestData req) {
        return Resp.ok(req);
    }

    @PostMapping("/post02")
    public Resp<?> test02(@RequestBody TestData req) {
        return Resp.ok(req);
    }

    public static void main(String[] args) throws Exception {
        String secretId = "eRAu9lIQHdc5ZA5lvC9F9M3JoEmRBShtBCxl";
        String secretKey = "e3r695TzopL3cK8D41KWBrPAjV2Yz0h9fiem";

        HttpRequest request01 = HttpUtil.createPost("http://127.0.0.1:38081/system-service/testSign/post");

        for (int i = 0; i < 20; i++) {
            TestData testData = new TestData();
            StopWatch started = StopWatch.createStarted();
            Map<String, String> map = SignUtils.genSign(SignType.MD5, secretId, secretKey, testData).toMapOfStringType();

            request01.addHeaders(map);
            request01.body(JSON.toJSONString(testData));

            HttpResponse execute = request01.execute();

            String body = execute.body();
            long time = started.getTime(TimeUnit.MILLISECONDS);

            JSONObject respBody = JSON.parseObject(body);
            SignPayload signPayload = SignPayload.buildByFunction(execute::header);
            SignUtils.checkSign(signPayload, secretKey, respBody);

            System.out.println("request01 body: " + body + ", time: " + time + " ms");
        }
    }
}
